//////////////////////////////////////////////////////////////////////////////////
// Who  : Lim, Dong-moon (dmlim@nate.com, 011-701-9381)
// When : 2005/11/29
//////////////////////////////////////////////////////////////////////////////////

#ifndef __NCRYPT_RSA_KEY_H__
#define __NCRYPT_RSA_KEY_H__

#include <windows.h>
#include <wincrypt.h>
#include <cryptuiapi.h>

#ifndef _WIN32_WCE
#include <crtdbg.h>
#endif

#include <tchar.h>
#include <nblob.h>
#include <ncrypt_verify.h>

BOOL ReadPEMPrivateKey(
	HCRYPTPROV hCryptProv, 
	LPCTSTR lpszPKFile, 
	LPCSTR lpszPasswd, 
	HCRYPTKEY* phPrivateKey);

void GetSupportedAlgs(
	HCRYPTPROV hCryptProv, 
	const ALG_ID* pPreferAlgs, 
	DWORD cPreferLen, 
	ALG_ID** ppSupportAlgs, 
	DWORD* pcSupportLen);

BOOL DecodeCertAndImportPublicKey(
	HCRYPTPROV hCryptProv, 
	const BYTE* pbCert, 
	DWORD cbCert, 
	HCRYPTKEY* phPubKey);

BOOL MyExportKey(
	HCRYPTKEY hKey,
	HCRYPTKEY hExpKey,
	DWORD dwBlobType,
	DWORD dwFlags,
	BYTE** ppbData,
	DWORD* pdwDataLen
);

BOOL MyDecrypt(
	HCRYPTKEY hKey,
	HCRYPTHASH hHash,
	BOOL Final,
	DWORD dwFlags,
	const BYTE* pbInData,
	DWORD dwInDataLen,
	BYTE** ppbOutData,
	DWORD* pdwOutDataLen
);

BOOL MyEncrypt(
	HCRYPTKEY hKey,
	HCRYPTHASH hHash,
	BOOL Final,
	DWORD dwFlags,
	const BYTE* pbInData,
	DWORD dwInDataLen,
	BYTE** ppbOutData,
	DWORD* pdwOutDataLen
);

#ifdef _WIN32_WCE
BOOL MyAcquireContext(
    HCRYPTPROV *phProv,
    LPCTSTR szContainer,
    LPCTSTR szProvider,
    DWORD dwProvType,
	BOOL isServer = FALSE
);
#else
BOOL MyAcquireContext(
    HCRYPTPROV *phProv,
    LPCSTR szContainer,
    LPCSTR szProvider,
    DWORD dwProvType,
	BOOL isServer = FALSE
);
#endif

BOOL HashRandomAndSessionKey(
	HCRYPTPROV hProv,
	DWORD cbServerRandom,
	const BYTE* pbServerRandom,
	HCRYPTKEY hSessionKey,
	DWORD* pcbClientHash,
	BYTE** ppbClientHash 
);

BOOL MyHash(	
	HCRYPTPROV hProv,
	ALG_ID algId,
	const BYTE* pbDataIn,
	DWORD cbDataIn,
	BYTE** ppbHashOut,
	DWORD* pcbDataOut
);

BOOL MyQueryAlgorithm(HCRYPTPROV hProv, ALG_ID algId, PROV_ENUMALGS_EX* pAlg);
DWORD GetAlgorithmKeySize(ALG_ID algId);
BOOL MyImportKey(HCRYPTPROV hProv, ALG_ID algId, const BYTE* pbKey, DWORD cbKey, HCRYPTKEY* phKey);
BOOL CHashSaltPswd(HCRYPTPROV hCryptProv, const BYTE* pbSalt, DWORD cbSalt, LPCTSTR passwd, BYTE** ppbHash, DWORD* pcbHash);
BOOL CHashSaltPswd40(HCRYPTPROV hCryptProv, const BYTE* pbSalt, DWORD cbSalt, LPCTSTR passwd, BYTE** ppbHash, DWORD* pcbHash);
BOOL MyDeriveKey(HCRYPTPROV hCryptProv, ALG_ID algId, const BYTE* pbSalt, DWORD cbSalt, LPCTSTR passwd, DWORD cbKey, BYTE** ppbKey);
BOOL VerifyCertificate(NBLOB* pCert, int nChainsLen, NBLOB* pChains, DWORD* pdwError, HostNameVerifyCallback fn, LPVOID ctx);
BOOL ShowCertificate(NBLOB* pCert, int nChainsLen, NBLOB* pChains, PCRYPTUI_VIEWCERTIFICATE_STRUCT pCertViewInfo);

#endif // __NCRYPT_RSA_KEY_H__
